package com.example.myapplication.okHttp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.myapplication.R;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;

public class OkHttpActivity extends AppCompatActivity {

    private OkHttpClient okHttpClient;
    private Retrofit retrofit;
    private HttpbinService httpbinService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ok_http);
        okHttpClient = new OkHttpClient();

        retrofit= new Retrofit.Builder().baseUrl("https://www.httpbin.org").build();
        httpbinService = retrofit.create(HttpbinService.class);
    }

    public void getSync(View view) {
        new Thread(){
            @Override
            public void run() {
                Request request = new Request.Builder().url("https://www.httpbin.org/get?a=1&b=2").build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    Log.i("TAG", "getSync: "+response.body().string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    public void getASync(View view) {
        Request request = new Request.Builder().url("https://www.httpbin.org/get?a=1&b=2").build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
              if(response.isSuccessful()){
                    Log.i("TAG", "getSync: "+response.body().string());
                }
            }
        });
    }

    public void postSync(View view) {
        new Thread(){
            @Override
            public void run() {
                FormBody formBody = new FormBody.Builder().add("a","1").build();
                Request request = new Request.Builder().url("https://www.httpbin.org/post").post(formBody).build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    Log.i("TAG", "postSync: "+response.body().string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }

    public void postASync(View view) {
//        FormBody formBody = new FormBody.Builder()
//                .add("a","1").build();
//        Request request = new Request.Builder().url("https://www.httpbin.org/post")
//                .post(formBody).build();
//        Call call = okHttpClient.newCall(request);
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(@NonNull Call call, @NonNull IOException e) {
//
//            }
//
//            @Override
//            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
//                if(response.isSuccessful()){
//                    Log.i("TAG", "postASync: "+response.body().string());
//                }
//            }
//        });
         Log.i("TAG", "onResponse: ==================================");
         retrofit2.Call<ResponseBody> call= httpbinService.post("huang","123456");
         call.enqueue(new retrofit2.Callback<ResponseBody>() {
             @Override
             public void onResponse(retrofit2.Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {

                 try {
                     Log.i("TAG", "onResponse: "+response.body().string());
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }

             @Override
             public void onFailure(retrofit2.Call<ResponseBody> call, Throwable t) {

             }
         });
    }
}